package com.itgone.presenter.impl;

import android.content.Intent;

import com.andview.refreshview.utils.LogUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.itgone.bean.Product;
import com.itgone.common.ServerResponse;
import com.itgone.model.IProductModel;
import com.itgone.model.impl.ProductModelImpl;
import com.itgone.presenter.IProductDetailPresenter;
import com.itgone.util.AppUtil;
import com.itgone.util.JsonUtil;
import com.itgone.view.IProductDetailView;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import okhttp3.Call;

import static com.itgone.common.Const.KEY_PRODUCT_ID;

public class ProductDetailPresenterImpl implements IProductDetailPresenter {

    private IProductDetailView productDetailView;

    private IProductModel productModel = new ProductModelImpl();

    public ProductDetailPresenterImpl(IProductDetailView productDetailView) {
        this.productDetailView = productDetailView;
    }

    @Override
    public void loadProductDetail() {
        Intent intent = productDetailView.getIntent();
        int productId = intent.getIntExtra(KEY_PRODUCT_ID, -1);

        if (productId == -1) {
            return;
        }
        productModel.getProduct(String.valueOf(productId), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                ServerResponse serverResponse = JsonUtil.parseResponse(response, Product.class);
                if (serverResponse.isSuccess()) {

                    Product data = (Product) serverResponse.getData();

                    //初始化产品详情WebView
                    String newContent = getNewContent(data.getDetail());
                    productDetailView.loadWebView(newContent);

                    //初始化产品图片展示ViewPage
                    String subImages = data.getSubImages();
                    String[] subImageArr = subImages.split(",");
                    productDetailView.loadViewPage(subImageArr);

                    productDetailView.setName(data.getName());
                    productDetailView.setPrice(data.getPrice().doubleValue());
                    productDetailView.setSubtitle(data.getSubtitle());

                } else {
                    LogUtils.e(serverResponse.toString());
                    ToastUtils.showToast(AppUtil.getContext(), serverResponse.getMsg());
                }
            }
        });

    }

    /**
     * 将html文本内容中包含img标签的图片，宽度变为屏幕宽度，高度根据宽度比例自适应
     **/
    private String getNewContent(String htmltext) {
        try {
            Document doc = Jsoup.parse(htmltext);
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width", "100%").attr("height", "auto");
            }

            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }
}
